from decimal import Decimal
from typing import TypedDict
import xlwt
from datetime import date
from xlwt import Workbook

from inventory.views.func.datatype import strip_steel_sale_table, angle_steel_manual_table
from inventory.views.func.table_css import css1


# 行数自增器
class Row:
    def __init__(self, val):
        self.num = val

    @property
    def add(self):
        temp = self.num
        self.num += 1
        return temp

    @add.setter
    def add(self, val):
        self.num = val

    def add1(self):
        self.num += 1


def make_now_strip_steel_table(true_data: list[strip_steel_sale_table], make_table_date: date):

    # 生成表格
    xl = xlwt.Workbook(encoding='utf-8')
    sheet1 = xl.add_sheet('带钢正品', cell_overwrite_ok=True)

    # 使用经典样式
    style, style_title = css1()

    # 设置单元格长度
    for i in [sheet1]:
        i.col(0).width = 2600
        i.col(1).width = 2600
        i.col(2).width = 2600
        i.col(3).width = 2600
        i.col(4).width = 2600
        i.col(5).width = 2600
        i.col(6).width = 2600
        i.col(7).width = 2600
        i.col(8).width = 2600

    # 表头
    def return_title(t_sheet, t_piece, t_weight):

        t_sheet.write_merge(0, 1, 0, 8, f'天津市兆博实业有限公司带钢正品库存表', style_title)

        t_sheet.write_merge(2, 2, 0, 1, '日期', style_title)
        t_sheet.write_merge(2, 2, 2, 3, f'{make_table_date}', style_title)
        t_sheet.write(2, 4, '总件数', style_title)
        t_sheet.write(2, 5, t_piece, style_title)
        t_sheet.write(2, 6, '总吨数', style_title)
        t_sheet.write(2, 7, t_weight, style_title)

        t_sheet.write(3, 0, '规格', style_title)
        t_sheet.write(3, 1, '材质', style_title)
        t_sheet.write(3, 2, '件数', style_title)
        t_sheet.write(3, 3, '重量', style_title)
        t_sheet.write(3, 5, '规格', style_title)
        t_sheet.write(3, 6, '材质', style_title)
        t_sheet.write(3, 7, '件数', style_title)
        t_sheet.write(3, 8, '重量', style_title)


    row_left = Row(4)
    row_right = Row(4)
    true_piece_total = 0
    true_weight_total = 0
    # 规格以234开头放在右侧，小于234的放在左侧
    for i in true_data:
        if i['model'][0:3] <= '234':
            sheet1.write(row_left.num, 0, i['model'], style_title)
            sheet1.write(row_left.num, 1, i['name'].replace('带钢', ''), style_title)
            sheet1.write(row_left.num, 2, i['whole_piece'], style_title)
            sheet1.write(row_left.num, 3, i['weight'], style_title)
            row_left.add1()
        else:
            sheet1.write(row_right.num, 5, i['model'], style_title)
            sheet1.write(row_right.num, 6, i['name'].replace('带钢', ''), style_title)
            sheet1.write(row_right.num, 7, i['whole_piece'], style_title)
            sheet1.write(row_right.num, 8, i['weight'], style_title)
            row_right.add1()
        true_piece_total += i['whole_piece']
        true_weight_total += i['weight']
    return_title(sheet1, true_piece_total, true_weight_total)
    return xl
    # xl.save(r'/home/xph/Desktop/公司共享文件夹/132.xls')


# 生成带钢自动库存表格式的数据
def strip_steel_auto_store_table(in_data: list[angle_steel_manual_table], out_data: list[angle_steel_manual_table]) -> Workbook:
    # 生成表格
    xl = xlwt.Workbook(encoding='utf-8')
    sheet1 = xl.add_sheet('入库', cell_overwrite_ok=True)
    sheet2 = xl.add_sheet('出库', cell_overwrite_ok=True)

    # 使用经典样式
    style, style_title = css1()

    # 设置单元格长度
    for k in [sheet1, sheet2]:
        k.col(0).width = 4000
        k.col(1).width = 4000
        k.col(2).width = 4000

    # 写入数据
    row = Row(0)
    for i in in_data:
        sheet1.write(row, 0, i['material__code'], style)
        sheet1.write(row, 1, i['whole_piece'], style)
        sheet1.write(row, 2, i['weight'], style)
        row.add1()

    row.num = 0
    for i in out_data:
        sheet2.write(row, 0, i['material__code'], style)
        sheet2.write(row, 1, i['whole_piece'], style)
        sheet2.write(row, 2, i['weight'], style)
        row.add1()

    return xl




